﻿using System;
using System.Collections.Generic;
using NHibernate;
using SimpleScrum.Core.Entities;
using SimpleScrum.Helpers;
using SimpleScrum.Utils;

namespace SimpleScrum.Areas.Scrum.Models
{
    public class TeamMemberModel
    {
        private ISession _session;

        public IList<TeamMember> GetTeamMembersByUsersId()
        {
            IList<TeamMember> teamMembers;
            using (_session = NHibernateHelper.GetSession())
            {
                //NHibernate query 
                IQuery query =
                    _session.CreateQuery("from TeamMember where UsersId='" + Client.Instance.ClientId + "';");
                teamMembers = query.List<TeamMember>();
            }
            return teamMembers;
        }

        public IList<TeamMember> GetUsersByTeamId(Guid teamId)
        {
            IList<TeamMember> usersInTeam;
            using (_session = NHibernateHelper.GetSession())
            {
                //NHibernate query 
                IQuery query =
                    _session.CreateQuery("from TeamMember where TeamId='" + teamId + "';");
                usersInTeam = query.List<TeamMember>();
            }
            return usersInTeam;
        }

        public TeamMember GetTeamMemberById(Guid id)
        {
            TeamMember team;
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    team = _session.Get<TeamMember>(id);
                    tran.Commit();
                }
            }
            return team;
        }

        public bool NewTeamMember(TeamMember team)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    _session.Save(team);
                    tran.Commit();
                }
            }
            return true;
        }

        public void UpdateTeamMember(TeamMember team)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    _session.Update(team);
                    tran.Commit();
                }
            }
        }

        public void DeleteTeamMember(TeamMember team)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    try
                    {
                        _session.Delete(team);
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        string msg = ex.Message;
                    }
                }
            }
        }
    }
}